package com.create.myworld.living.people;

import com.create.myworld.action.PeopleAction;
import com.create.myworld.annotations.NotDo;
import com.create.myworld.enums.Sex;
import com.create.myworld.objects.derive.substance.Living;
import com.create.myworld.objects.derive.substance.SubObject;
import com.create.myworld.objects.derive.substance.Work;
import com.create.myworld.util.RabbitMQUtil;
import com.create.myworld.worldthings.WorldThings;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zcw
 * 人类基类
 */
public class AncestorPeople extends Living implements PeopleAction {

    /**
     * 金币
     */
    public long coin;

    /**
     * 持有物品
     */
    public List<SubObject> subObjectList = new ArrayList();

    /**
     * 打工剩余回合数
     */
    public long workRound;

    /**
     * 待获取金币数
     */
    public long canGetCoin;

    public AncestorPeople() {
    }

    /**
     * 人类诞生
     *
     * @param name
     * @param liveTime
     */
    public AncestorPeople(Class myself, Sex sex, String surname, String name, Long liveTime) {
        super(myself, sex, surname, name, liveTime);
    }


    @Override
    public void sale() {
        RabbitMQUtil.pushNormal(getName() + "在交易,正在选择交易对象...");
        AncestorPeople salePerson = WorldThings.getSameClassLiving(this.getClass(), this);
        if (salePerson == null) {
            RabbitMQUtil.pushNormal(getName() + "找不到交易对象,交易终止");
            return;
        }
        if (salePerson.workRound > 0) {
            RabbitMQUtil.pushNormal(getName() + "的交易对象" + salePerson.getName() + "正在打工,无法进行交易");
            return;
        }
        RabbitMQUtil.pushNormal(getName() + "与" + salePerson.getName() + "开始交易");
        /**
         * 随机抽取物品进行交易,若没有物品,则抽取交易对象的物品与自己交易,若金币不足,则触发打工
         */
        Integer subObjectIndex = null;
        if (subObjectList != null && subObjectList.size() > 0) {
            for (int i = 0; i < subObjectList.size(); i++) {
                SubObject object = subObjectList.get(i);
                if (object != null) {
                    subObjectIndex = i;
                } else {
                    continue;
                }
            }
            if (subObjectIndex != null) {
                /**
                 * 交易
                 */
                doSale(salePerson, this, subObjectIndex);
            }
        } else {
            for (int i = 0; i < salePerson.subObjectList.size(); i++) {
                SubObject object = salePerson.subObjectList.get(i);
                if (object != null) {
                    subObjectIndex = i;
                } else {
                    continue;
                }
            }
            if (subObjectIndex != null) {
                /**
                 * 交易
                 */
                doSale(this, salePerson, subObjectIndex);
            }
        }
    }


    @Override
    public void work() {
        /**
         * 抽取工作
         */
        Work work = WorldThings.getWork();
        this.workRound = work.getWorkRound();
        this.canGetCoin = work.getCoin();
        this.status = 2;
        RabbitMQUtil.pushNormal(getName() + "开始打工,工作内容:" + work.getName());
    }

    @Override
    public void winCoin() {
        this.coin += 500;
        RabbitMQUtil.pushNormal(getName() + "狗运爆发,中了500彩票 当前金币:" + coin);
    }

    @Override
    public void pickUpObject() {
        String objectName = WorldThings.giveObject(this);
        RabbitMQUtil.pushNormal(getName() + "狗运爆发,捡到一个" + objectName + " " + "当前物品:" + subObjectList.stream().map((subObject) -> {
            return subObject.getName();
        }).collect(Collectors.joining(",", "[", "]")));
    }

    /**
     * 进行交易
     */
    private void doSale(AncestorPeople buy, AncestorPeople sale, int objectIndex) {
        SubObject object = sale.subObjectList.get(objectIndex);
        if (buy.coin > object.saleCoin) {
            buy.coin -= object.saleCoin;
            sale.coin += object.saleCoin;
            buy.subObjectList.add(object);
            RabbitMQUtil.pushNormal(buy.getName() + "以" + object.saleCoin + "金币的价格买下了" + object.getName() + "," + sale.getName() + "赚取了" + object.saleCoin + "金币");
        } else {
            RabbitMQUtil.pushNormal(buy.getName() + "想买" + object.getName() + ",可是金币不够,触发打工行为");
            buy.work();
        }
    }

    /**
     * 继承父母遗产
     */

    @NotDo
    public void extend(AncestorPeople people) {
        this.coin += people.coin;
        this.subObjectList.addAll(people.subObjectList);
        RabbitMQUtil.pushNormal(getName() + "继承了" + people.getName() + "的遗产:" + people.coin + "金币,当前金币:" + coin);
        RabbitMQUtil.pushNormal(getName() + "继承了" + people.getName() + "的物品:" + people.subObjectList.stream().map((subObject -> {
            return subObject.getName();
        })).collect(Collectors.joining(",", "[", "]")) + ",当前物品:" + subObjectList.stream().map(subObject -> {
            return subObject.getName();
        }).collect(Collectors.joining(",", "[", "]")));
    }

}
